package edu.tongji.engine.exe;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import edu.tongji.engine.def.ForkNode;
import edu.tongji.engine.def.ProcessDefinition;
import edu.tongji.engine.def.Transition;


public class ProcessInstance {
	
	private Long id;
	
	private ProcessDefinition pd;
	
	private String name;
	
	//private ExecutionContext context;
	
	private Set<Token> tokens = new HashSet<Token>(0);
	
	private Set<ExecutionContext> contexts = new HashSet<ExecutionContext>(0);
	
	//private ProcessService processService = new ProcessService();
	
	public ProcessInstance(){
		
	}
//	public ProcessInstance( String processName ){
//		//.......intial process from db, create token according to processdefinition, 
//		//initialize context, assign context to token.executioncontext
//		pd = processService.getProcessDefinition(processName);
//		context = new ExecutionContext();//get info from context db table
//		
//		token = new Token();
//		token.setNode( pd.getRootNode());
//		token.setContext(context);
//		token.setPi(this);		
//	}
//	
//	public void next(){
//		this.token.next();
//	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ProcessDefinition getPd() {
		return pd;
	}

	public void setPd(ProcessDefinition pd) {
		this.pd = pd;
	}

	public Set<Token> getTokens() {
		return tokens;
	}

	public void setTokens(Set<Token> tokens) {
		this.tokens = tokens;
	}

	public Set<ExecutionContext> getContexts() {
		return contexts;
	}

	public void setContexts(Set<ExecutionContext> contexts) {
		this.contexts = contexts;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Token getToken(){
		return tokens.iterator().next();
	}
	
	public void setToken(Token token){
		this.tokens.add(token);
		token.setPi(this);
	}
//	public List<Token> generateChildToken(Token parent){
//		Set<Transition> arrivingTransitons = ((ForkNode)(parent.getNode())).getLeavingTransitons();
//		List<Token> childTokens = new ArrayList();
//		for (Transition trans : arrivingTransitons){
//			Token child = new Token();
//			child.setNode(trans.getToNode());
//			child.setContext(parent.getContext());
//			child.setPi(this);
//			childTokens.add(child);
//		}
//		return childTokens;
//	}
	
	//update() just need to update token table and instance table
	
}
